package cn.enjoy.rabbitmq.handler.boot;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 死信队列消费者
 *
 * @author K
 * @date 2021/8/26 上午10:14
 */
@Component
@Slf4j
public class DLXQueueHandler {

    @RabbitListener(queues = "DLX_QUEUE")
    public void DLXReceive(Map str, Message message, Channel channel) throws JsonProcessingException {
        log.info("死信监听：接收到消息:[{}]", str);
        try {
            //参数为：消息的DeliveryTag，是否批量拒绝，是否重新入队
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("签收失败", e);
        }
    }

//    @RabbitListener(queues = "NORMAL_QUEUE")
//    public void normalReceive(String str, Message message, Channel channel) throws JsonProcessingException {
//        log.info("正常监听：接收到消息:[{}]", str);
//        try {
//            //参数为：消息的DeliveryTag，是否批量拒绝，是否重新入队
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
//            log.info("拒绝签收...消息的路由键为:[{}]", message.getMessageProperties().getReceivedRoutingKey());
//        } catch (Exception e) {
//            log.error("消息拒绝签收失败", e);
//        }
//    }

}
